package com.ele.client.activity.editor.widget;

import com.ele.model.Answer;
import com.ele.model.Question;
import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.ControlGroup;
import com.github.gwtbootstrap.client.ui.FileUpload;
import com.github.gwtbootstrap.client.ui.TextArea;
import com.github.gwtbootstrap.client.ui.event.CloseEvent;
import com.github.gwtbootstrap.client.ui.event.CloseHandler;
import com.github.gwtbootstrap.client.ui.event.ClosedEvent;
import com.github.gwtbootstrap.client.ui.event.ClosedHandler;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.*;

import java.util.ArrayList;
import java.util.List;

public class QuestionEditorWidget extends Composite implements IsEditorWidget<Question> {

    interface DefaultUiBinder extends UiBinder<Widget, QuestionEditorWidget> {}
    private static DefaultUiBinder uiBinder = GWT.create(DefaultUiBinder.class);

    @UiField
    TextArea questionTextArea;
    @UiField
    Button addAnswerButton;
    @UiField
    Button deleteButton;
    @UiField
    FlowPanel answersPanel;
    @UiField
    Button generateAnswerButton;
    @UiField
	Button imageButton;
	@UiField
	ControlGroup imageControlGroup;
	@UiField
	FormPanel form;
	@UiField
	FileUpload fileUpload;

	private Long questionId;

    private List<FocusWidget> controls = new ArrayList<FocusWidget>();

    public QuestionEditorWidget(Question question) {
        initWidget(uiBinder.createAndBindUi(this));

        setValue(question);

        controls.add(questionTextArea);

		fileUpload.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
//				fileUpload.getElement().<InputElement>cast().click();
				form.submit();
			}
		});

		form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
			@Override
			public void onSubmitComplete(FormPanel.SubmitCompleteEvent event) {
//				Window.alert(event.getResults());
			}
		});

		form.addSubmitHandler(new FormPanel.SubmitHandler() {
			@Override
			public void onSubmit(FormPanel.SubmitEvent event) {
//				Window.alert("onSubmit");
			}
		});
    }

    @Override
    public HandlerRegistration addCloseHandler(CloseHandler handler) {
        return addHandler(handler, CloseEvent.getType());
    }

    @Override
    public HandlerRegistration addClosedHandler(ClosedHandler handler) {
        return addHandler(handler, ClosedEvent.getType());
    }

    @Override
    public void setValue(Question value) {
        if (value != null) {
            questionId = value.getId();

            questionTextArea.setValue(value.getText());
//            imgUrlTextBox.setValue(value.getImgUrl());

            List<Answer> answers = value.getAnswers();
            if (answers != null && !answers.isEmpty()) {
                for (Answer answer : answers) {
                    addAnswerControl(answer);
                }
            }
        }
    }

    public void addAnswerControl(Answer answer) {
        IsEditorWidget<Answer> widget = new AnswerEditorWidget(answer);
        widget.addClosedHandler(new ClosedHandler() {
            @Override
            public void onClosed(ClosedEvent closedEvent) {
                answersPanel.remove((IsWidget) closedEvent.getSource());
            }
        });
        answersPanel.add(widget);
        widget.setFocus(true);
    }

    @Override
    public Question getValue() {
        Question value = new Question();
        value.setId(questionId);
        value.setText(questionTextArea.getValue());
//        value.setImgUrl(imgUrlTextBox.getValue());

        List<Answer> answers = new ArrayList<Answer>();
        for (int i = 0; i < answersPanel.getWidgetCount(); i++) {
            IsEditorWidget<Answer> widget = (IsEditorWidget<Answer>) answersPanel.getWidget(i);
            Answer answer = widget.getValue();
            answer.setQuestion(value);
            answers.add(answer);
        }
        value.setAnswers(answers);

        return value;
    }

    @Override
    public void setFocus(boolean focused) {
        questionTextArea.setFocus(focused);
    }

    @UiHandler("deleteButton")
    void onDeleteClick(ClickEvent event) {
        fireEvent(new ClosedEvent());
    }

    @UiHandler("addAnswerButton")
    void onAddAnswerButtonClick(ClickEvent event) {
        addAnswerControl(null);
    }

    @UiHandler("generateAnswerButton")
    void onAddGenerateButtonClick(ClickEvent event) {
        fireEvent(new GenerateButtonClickEvent(questionTextArea.getText()));
    }

	@UiHandler("imageButton")
	void onImageButtonClick(ClickEvent event) {
		imageControlGroup.setVisible(!imageControlGroup.isVisible());
	}

    public HandlerRegistration addGenerationButtonHandler(GenerateButtonClickEvent.Handler handler) {
        return super.addHandler(handler, GenerateButtonClickEvent.TYPE);
    }
}
